package com.itkaien.service.impl;
import com.github.pagehelper.PageInfo;
import com.itkaien.mapper.GoodsMapper;
import com.itkaien.pojo.Goods;
import com.itkaien.service.GoodsService;;
import java.time.Instant;
import java.time.LocalDateTime;;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public void addgood(Goods goods) {
        LocalDateTime localDateTime = LocalDateTime.now();
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        goods.setCreateTime(date);
        goodsMapper.insert(goods);
    }

    @Override
    public void updateByidGood(Goods goods) {
        LocalDateTime localDateTime = LocalDateTime.now();
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        goods.setUpdateTime(date);
        goodsMapper.updateByPrimaryKey(goods);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        goodsMapper.deleteBatch(ids);
    }
    @Override
    public PageInfo<Goods> selectpageall(String name,int pageNum, int pageSize) {

        PageInfo<Goods> pageInfo = new PageInfo<>();
        int start = (pageNum - 1) * pageSize;
        List<Goods> goods = goodsMapper.selectlikeBypage(name, start, pageSize);
        long total = goods.size(); // 获取总记录数
        pageInfo.setList(goods);
        pageInfo.setTotal(total);
        pageInfo.setStartRow(start);
        pageInfo.setPageSize(pageSize);
        return pageInfo;

    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public Goods selectoneGood(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }
}
